﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.ComponentModel;
using System.Windows.Media;
using Citrix.Cmp;
using System.Windows.Input;

namespace Citrix.XaAdminMobile.ViewModel
{
    /// <summary>
    /// This button type enum controls the colouring of the button. It is the combination of
    /// this type and the target dialog that controls the exact colour.
    /// </summary>
    internal enum PopupButtonAppearance
    {
        /// <summary>
        /// Default button colour.
        /// </summary>
        Default,

        /// <summary>
        /// Plain, understated colour for close/cancel type actions.
        /// </summary>
        CloseCancel,

        /// <summary>
        /// Stronger, contrasting button colour to warn of an important, potentially
        /// irreversible change.
        /// </summary>
        Warning,
    }

    /// <summary>
    /// Button types used for creating simple pre-baked dialog buttons.
    /// </summary>
    internal enum PopupButtonType
    {
        Cancel,

        OK
    }

    /// <summary>
    /// Represents a button on the poup up view.
    /// </summary>
    internal class PopupButtonViewModel
    {
        private static SolidColorBrush RedBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FF8E0000"));

        private static SolidColorBrush PlainBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FFBEBEBE"));

        private static SolidColorBrush BlackBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("Black"));

        private static SolidColorBrush WhiteBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("White"));

        /// <summary>
        /// The button type defines the brushes we use to draw the brush (in combination with
        /// the sync/async state taken from popupParent).
        /// </summary>
        private readonly PopupButtonAppearance brushType;

        /// <summary>
        /// Creates a simple pre-defined button. The colouring and command for the button are
        /// derived from the supplied type. All buttons will close the popup window. An OK
        /// button sets the DialogResult to true, and the cancel buttons sets it to false.
        /// </summary>
        /// <param name="window">The associated popup window.</param>
        /// <param name="type">The button type.</param>
        /// <exception cref="ArgumentNullException">Thrown if the popup window is null.</exception>
        public PopupButtonViewModel(PopupWindow window, PopupButtonType type)
        {
            if (null == window)
            {
                throw new ArgumentNullException("window");
            }

            if (type == PopupButtonType.Cancel)
            {
                this.brushType = PopupButtonAppearance.CloseCancel;
                this.PopupLabel = "Cancel";
                this.PopupCommand = new DelegateCommand(() =>
                {
                    window.DialogResult = false;
                    window.Close();
                });
            }
            else
            {
                this.brushType = PopupButtonAppearance.Default;
                this.PopupLabel = "OK";
                this.PopupCommand = new DelegateCommand(() =>
                {
                    window.DialogResult = true;
                    window.Close();
                });
            }
        }

        /// <summary>
        /// Creates a custom button for a popup window. You have to supply your own command.
        /// </summary>
        /// <param name="command">The command associated with this button.</param>
        /// <param name="label">The label for this button.</param>
        /// <param name="buttonType">Defines the colouring of the brush.</param>
        /// <param name="topMargin">The top margin in DIUs</param>
        /// <param name="bottomMargin">The bottom margin in DIUs</param>
        /// <exception cref="ArgumentNullException">Thrown if any parameter is null/empty.</exception>
        public PopupButtonViewModel(ICommand command, string label, PopupButtonAppearance buttonType)
        {
            if (null == command)
            {
                throw new ArgumentNullException("command");
            }
            if (string.IsNullOrEmpty(label))
            {
                throw new ArgumentNullException("label");
            }
            this.PopupCommand = command;
            this.PopupLabel = label;
            this.brushType = buttonType;
        }

        /// <summary>
        /// The command associated with this popup item.
        /// </summary>
        public ICommand PopupCommand
        {
            get;
            private set;
        }

        /// <summary>
        /// The label to display on the button.
        /// </summary>
        public string PopupLabel
        {
            get;
            private set;
        }

        /// <summary>
        /// The background brush for the button - used to set the base colour.
        /// </summary>
        public Brush BackgroundBrush
        {
            get
            {
                Brush brush = null;

                switch (this.brushType)
                {
                    case PopupButtonAppearance.CloseCancel:
                        {
                            brush = BlackBrush;
                            break;
                        }
                    case PopupButtonAppearance.Default:
                        {
                            brush = PlainBrush;
                            break;
                        }
                    case PopupButtonAppearance.Warning:
                        {
                            brush = RedBrush;
                            break;
                        }
                }

                return brush;
            }
        }

        /// <summary>
        /// The foreground brush for the button.
        /// </summary>
        public Brush ForegroundBrush
        {
            get
            {
                Brush brush = null;
                switch (this.brushType)
                {
                    case PopupButtonAppearance.CloseCancel:
                        {
                            brush = WhiteBrush;
                            break;
                        }
                    case PopupButtonAppearance.Default:
                        {
                            brush = BlackBrush;
                            break;
                        }
                    case PopupButtonAppearance.Warning:
                        {
                            brush = WhiteBrush;
                            break;
                        }
                }
                return brush;
            }
        }
    }
}
